package ui;

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import preprocess.XmlTransfer;
import utils.Configuration;

/**
 * XmlTransfer的UI控制类
 * 
 * @author huangcd
 * 
 */
public class StreamControlFrame extends JInternalFrame implements
        ActionListener
{
    private static final long serialVersionUID = -7169137188041856149L;
    private static String     rootPath;
    private static Dimension  defaultSize;
    private JButton           startButton;
    private JPanel            speedPanel;
    private JTextField        speedTextField;
    private JButton           speedButton;
    private JButton           stopButton;
    private JButton           resumeButton;
    private JButton           getFileDialog;
    private XmlTransfer       tran;

    static
    {
        rootPath = Configuration.RootPath;
        defaultSize = new Dimension(150, 150);
    }

    public StreamControlFrame()
    {
        super("XML流控制", true, false, true, true);
        setSize(defaultSize);
        setResizable(false);
        setLayout(new BorderLayout());
        add(getStartButton(), BorderLayout.CENTER);
        setVisible(true);
    }

    private JPanel getSpeedPanel()
    {
        if (speedPanel == null)
        {
            speedPanel = new JPanel();
            GridLayout layout = new GridLayout(3, 2);
            layout.setHgap(5);
            layout.setVgap(5);
            speedPanel.setLayout(layout);
            speedPanel.add(getSpeedTextField());
            speedPanel.add(getSpeedButton());
            speedPanel.add(getStopButton());
            speedPanel.add(getResumeButton());
            speedPanel.add(getFileDialog());

        }
        return speedPanel;
    }

    /**
     * @return the speedTextField
     */
    private JTextField getSpeedTextField()
    {
        if (speedTextField == null)
        {
            speedTextField = new JTextField();
        }
        return speedTextField;
    }

    /**
     * @return the stopButton
     */
    private JButton getStopButton()
    {
        if (stopButton == null)
        {
            stopButton = new JButton("停止");
            stopButton.addActionListener(this);
        }
        return stopButton;
    }

    private JButton getResumeButton()
    {
        if (resumeButton == null)
        {
            resumeButton = new JButton("继续");
            resumeButton.addActionListener(this);
        }
        return resumeButton;
    }

    /**
     * @return the speedButton
     */
    private JButton getSpeedButton()
    {
        if (speedButton == null)
        {
            speedButton = new JButton("更新");
            speedButton.addActionListener(this);
        }
        return speedButton;
    }

    private JButton getStartButton()
    {
        if (startButton == null)
        {
            startButton = new JButton("开始发送数据");
            startButton.addActionListener(this);
        }
        return startButton;
    }

    private JButton getFileDialog()
    {
        if (getFileDialog == null)
        {
            getFileDialog = new JButton("选择新数据源:");
            getFileDialog.addActionListener(this);
        }
        return getFileDialog;
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource().equals(startButton))
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    tran = new XmlTransfer(rootPath);
                    tran.xmlControl();
                    XmlTransfer.isGoOn = true;
                }
            });
            remove(startButton);
            add(getSpeedPanel(), BorderLayout.CENTER);
            setTitle("速度控制");
            updateUI();
        }
        else if (e.getSource().equals(speedButton))
        {
            try
            {
                XmlTransfer.timeInterval = new Long(getSpeedTextField()
                        .getText().trim());
            }
            catch (Exception e2)
            {
                JOptionPane.showMessageDialog(this, "速度只能是整数", "错误",
                        JOptionPane.ERROR_MESSAGE);
            }
        }
        else if (e.getSource().equals(stopButton))
        {
            // TODO: 停止发送数据
            if (stopButton.isEnabled())
            {
                XmlTransfer.isGoOn = false;
                stopButton.setEnabled(false);
                resumeButton.setEnabled(true);
            }
            else
            {

            }
        }
        else if (e.getSource().equals(resumeButton))
        {
            if (resumeButton.isEnabled())
            {
                XmlTransfer.isGoOn = true;
                stopButton.setEnabled(true);
                resumeButton.setEnabled(false);
            }
            else
            {

            }
        }
        else if (e.getSource().equals(getFileDialog))
        {
            JFileChooser fileChooser = new JFileChooser(".");
            fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            fileChooser.setDialogTitle("打开文件夹");
            int ret = fileChooser.showOpenDialog(null);
            XmlTransfer.listenDir = fileChooser.getSelectedFile()
                    .getAbsolutePath()
                    + "\\";
            fileChooser.setVisible(true);
        }
    }
}
